<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
    <title></title>
</head>
<body>
<!--弹窗：编辑-->
<div th:fragment="atruleRule" class="modal inmodal fade" id="atruleRule_Modal"
     tabindex="-1" role="dialog" aria-hidden="true" data-backdrop="static">
    <div class="modal-dialog modal-lg">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span
                        class="sr-only">Close</span></button>
                <h4 class="modal-title">编辑例外</h4>
                <small class="font-bold"></small>
            </div>
            <div class="modal-body">
                <div class="row">
                    <div class="col-md-12">
                        <form role="form" id="atruleRule_form">
                            <input id="atruleRule_id" name="id" type="hidden">
                            <div class="form-group"><label>工作时间间隔（分钟）</label>
                                <input id="atruleRule_interval" name="interval" type="text"
                                       placeholder="工作时间间隔" class="form-control">
                            </div>
                            <div class="form-group"><label>是否自动生成工作时间</label>
                                <div class="i-checks">
                                    <label> <input type="radio" value="true" name="isAuto"> <i></i>
                                        是
                                    </label>&nbsp;&nbsp;
                                    <label> <input type="radio" value="false" name="isAuto"> <i></i>
                                        否
                                    </label>
                                </div>
                            </div>
                        </form>
                    </div>
                </div>
            </div>

            <div class="modal-footer">
                <button type="button" class="btn btn-white" data-dismiss="modal">关闭</button>
                <button type="button" class="btn btn-primary" onclick="atruleRuleObj.submitHandler()">
                    保存
                </button>
            </div>
        </div>
    </div>

    <script type="text/javascript">
        $(function () {
            $('.i-checks').iCheck({
                checkboxClass: 'icheckbox_square-green',
                radioClass: 'iradio_square-green',
            });
            $('.i-checks').on('ifChecked', function (event) {
                console.log(event)
            });
            atruleRuleObj.myValidator = atruleRuleObj.myForm.validate({
                rules: atruleRuleObj.dataRule,
                submitHandler: function (form) {
                    atruleRuleObj.dataFormSubmit(form);
                }
            });
            atruleRuleObj.myModal.on('hide.bs.modal', function (event) {
                atruleListObj.refreshDataList();
            })
        })
        let atruleRuleObj = new Object({
            myValidator: undefined,
            ruleId: undefined,
            myForm: $('#atruleRule_form'),
            myModal: $('#atruleRule_Modal'),
            dataRule: {
                interval: {required: true},
                isAuto: {required: true}
            },
            init: function (userCode) {
                $.myGet({
                    url: `/appoin/atrule/info/${userCode}`,
                    success: function (data) {
                        if (data && data.code === 0) {
                            let atrule = data.atrule;
                            atruleRuleObj.myModal.find(".modal-title").text(atrule.realName+' 预约规则');
                            atruleRuleObj.myForm[0].reset();
                            for (let i in atrule) {
                                if (atruleRuleObj.myForm.find(`[name=${i}]`).length > 0) {
                                    atruleRuleObj.myForm.find(`[name=${i}]`).val(atrule[i])
                                }
                            }
                            // $(`input:radio[name="isAuto"][value=${atrule.isAuto}]`).iCheck('check');
                            $('#atruleRule_Modal').modal('show')
                        } else {
                            toastr.error(data.msg, '操作失败')
                        }
                    }
                })
            },
            submitHandler: function () {
                atruleRuleObj.myForm.submit();
            },
            dataFormSubmit: function (form) {
                let formVals = $(form).serializeArray();console.log(formVals)
                let data = {}
                for (let i = 0; i < formVals.length; i++) {
                    data[formVals[i].name] = formVals[i].value;
                }
                $.myPost({
                    url: `/appoin/atrule/${data.id ? 'update' : 'save'}`,
                    dataType: 'json',
                    data: data,
                    success: function (data) {
                        if (data && data.code === 0) {
                            toastr.success(data.msg, '操作提示')
                            atruleRuleObj.myModal.modal('hide')
                        } else {
                            toastr.error(data.msg, '操作失败')
                        }
                    }
                })
            }
        })
    </script>
</div>
</body>
</html>